﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;

public partial class sshd : BasePage
{
    MessageBT mbt = new MessageBT();
    public int pagesize = 8;
    public int pages;
    public int page;
    public string page_url;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["type"] == null)
        {
            Response.Redirect("login.aspx");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["page"] != null)
            {
                string p = Request["page"];
                page = int.Parse(p);
            }
            else
            {
                page = 1;
            }
            page = page <= 0 ? 1 : page;
            page_url = Request.Url.ToString().Split('?')[0];
            page_url += "?page=[i]";
            controlRepeater1();
        }

    }

    private void controlRepeater1()
    {
        DataTable table1 = mbt.FindMessage();
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = table1.DefaultView;
        pds.PageSize = pagesize;
        pds.AllowPaging = true;
        page = page <= 0 ? 1 : page;
        page_url = Request.Url.ToString().Split('?')[0];
        page_url += "?page=[i]";
        pds.CurrentPageIndex = page - 1;
        int dataTotal = table1.Rows.Count;
        pages = dataTotal % pagesize == 0 ? dataTotal / pagesize : dataTotal / pagesize + 1;
        pages = pages == 0 ? 1 : pages;
        this.Repeater1.DataSource = pds;
        this.Repeater1.DataBind();
    }

    public string CheckType(string type)
    {
        if (type =="admin")
        {
            return " style='Color:blue;' ";
        }
        else
        {
            return " style='Color:black;' ";
        }
    }

    public string getTypeName(string type)
    {
        if (type == "admin")
        {
            return "教师";
        }
        else
        {
            return "学生";
        }
    }

    protected void button_Click(object sender, EventArgs e)
    {
        string type = Convert.ToString(Session ["type"].ToString ());
        string text1 = this.txb.Text;
        if (text1.Trim().Length == 0)
        {
            this.lbmeg2.Text = "内容不能为空";
            return;
        }
        string created_by;
        int id;
        if (type == "admin")
        {
            AdminBT abt = new AdminBT();
            id = Convert.ToInt32(Session["admin_id"].ToString());
            created_by = abt.FindbyId(id);
        }
        else 
        {
            StudentBT sbt = new StudentBT();
            id = Convert.ToInt32(Session ["student_id"].ToString ());
            DataRow dr = sbt.FindById(id);
            if (dr["name"] != null && dr["name"].ToString().Trim().Length != 0)
            {
                created_by = dr["name"].ToString();
            }
            else
            {
                created_by = dr["account"].ToString();
            }
        }

        bool bl = mbt.InsertText(text1 ,type,created_by );
        if (bl)
        {
            this.lbmeg2.Text = "提交成功";
            this.txb.Text = "";
            controlRepeater1();
        }
        else 
        {
            this.lbmeg2.Text = "操作失败";
        }
        
       
    }
}